/**
 * Created by CodeTlan.
 * User: hellreuter
 * Date: 3/20/12
 * Time: 1:18 AM
 */
Ext.define('CT.controller.Contratos', {
    extend: 'Ext.app.Controller',
    views:[
        'contrato.Window',
        'contrato.Grid',
        'contrato.Form'
    ],
    stores:['Contratos'],
    models:['Contrato'],
    requires: [
        'Ctc.view.ComboPaging',
        'Ctc.view.DateMX',
        'CT.view.departamento.ComboDepartamentoUsuario',
        'CT.view.categoria.Combo'
    ],
    refs:[{
        ref:'combodepartamentosusuarios',
        selector:'combodepartamentosusuarios'
    }],
    win: false,
    init: function(){
        this.control({
            'formcontrato button[action=add]':{
                click: this.agregar
            },
            'gridcontrato':{
                edit: this.editar,
                cellclick: this.clicked
            },
            'gridcontrato checkcolumn':{
                checkchange: this.activar
            }
        });
    },
    lanzarVentana:function(anim, icon){
        if(!this.win){
            this.init();
            this.win=Ext.widget('wincontratos',{animateTarget:anim,iconCls:icon});
        }
        this.win.show();
    },
    clicked:function(grid, cell, index, record){
        switch(index){
            case 9: this.eliminar(grid,record.data);break;
        }
    },
    agregar: function(button){
        var form = button.up('form').getForm();
        var store = this.getContratosStore();
        if(form.isValid()){
            form.submit({
                url: 'index.php/contratos/add',
                success: function(){
                    form.reset();
                    Notification.ok('Contrato Agregado',' ');
                    store.load();
                },
                failure: function(){
                    Notification.error('No es posible agregar contrato', ' ');
                }
            });
        }
    },
    editar:function(editor, e){
        var valor=Ext.isDate(e.value) ? Ext.util.Format.date(e.value, 'Y-m-d') : e.value;
        var mods='{"'+e.field+'":"'+valor+'"}';
        Ext.Ajax.request({
            url: 'index.php/contratos/edit',
            params: {
                mod: mods,
                contrato_k:e.record.data.contrato_k
            },
            success: function(response){
                e.record.commit();
                Notification.ok('Contrato Modificado',' ');
            }
        });
    },
    activar: function(check, index, activo){
        var store = this.getContratosStore();
        var record = store.getAt(index);
        Ext.Ajax.request({
            url: 'index.php/contratos/active',
            params: {
                contrato_k: record.data.contrato_k,
                activo: activo?1:0
            },
            success: function(response){
                record.commit();
                Notification.ok('Contrato activado');
            }
        });
    },
    eliminar:function(grid,record){
        Ext.Msg.confirm('Acción requerida','Está seguro que desea eliminar el contrato: '+record.nombre+'?',function(r){
            if(r=="yes"){
                var store=this.getContratosStore();
                Ext.Ajax.request({
                    url: 'index.php/contratos/delete',
                    params: {
                        contrato_k: record.contrato_k
                    },
                    success: function(response){
                        var sm = grid.getSelectionModel();
                        store.remove(sm.getSelection());
                        Notification.ok('Contrato Eliminado','Eliminado');
                    }
                });
            }
        },this);
    }
});